package com.cal.calonecal.utils;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cal.calonecal.mapper.EquationMapper;
import com.cal.calonecal.mapper.MappingEquationExamMapper;
import com.cal.calonecal.pojo.entity.EquationEntity;
import com.cal.calonecal.pojo.entity.MappingEquationExam;
import com.cal.calonecal.pojo.vo.EquationVO;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 76004
 * @apiNote
 * @date 2023/5/10 16:25
 */
@Component
@ConfigurationProperties(prefix = "equation")
@Data
public class EquationUtil {
    @Autowired
    private EquationMapper equationMapper;

    @Autowired
    private EquationBuilder equationFactory;

    @Autowired
    private MappingEquationExamMapper mappingEquationExamMapper;

    @Autowired
    private RedisTemplate<String, String>redisTemplate;

    private Double rate;

    /***
     * 按照难度和数目生成算式, 其中百分之六十的算式为当前难度，向上取整
     * @param level
     * @param num
     * @return
     */
    public List<EquationVO> createEquations(Integer level, Integer num, Long eid){
        List<EquationVO> equationExamVOS = new ArrayList<>();
        //当前难度题目数目
//        int currentLevelNum = (int) (num*this.rate == (int)(num*this.rate)?num*this.rate:num*this.rate+1);
        int currentLevelNum = num;

        //数据库中当前难度等级的算式数目
        int dataNum = (int) (equationMapper.selectCount(new QueryWrapper<EquationEntity>().eq("level",level)));
        //需要新创建的当前难度算式数目，当需要生成的算式数目为50，数据库算式数目达到10000时停止新生成算式,向下取整
        int needDataBaseNum = currentLevelNum-currentLevelNum*dataNum/10000;
        //创建当前等级的算式并添加到数据库
        for(int i = 1; i <= needDataBaseNum; i++){
            EquationVO equationExamVO = null;
            switch (level){
                case 1:
                    equationExamVO = equationFactory.createLel1();
                    break;
                case 2:
                    equationExamVO = equationFactory.createLel2();
                    break;
                case 3:
                    equationExamVO = equationFactory.createLel3();
                    break;
                case 4:
                    equationExamVO = equationFactory.createLel4();
                    break;
                case 5:
                    equationExamVO = equationFactory.createLel5();
                    break;
                case 6:
                    equationExamVO = equationFactory.createLel6();
                    break;
            }
            if(equationExamVO == null) {
                throw new RuntimeException("等级类型错误");
            }
            equationExamVOS.add(equationExamVO);
            equationExamVO.setId(getEquationId());
            EquationEntity equationEntity = BeanUtil.copyProperties(equationExamVO, EquationEntity.class);
            //保存算式
            equationMapper.insert(equationEntity);
        }

        //从数据库随机获取当前难度的算式
        equationExamVOS.addAll(equationMapper.selectByLevelRand(level,currentLevelNum-needDataBaseNum));
        for(EquationVO equationVO:equationExamVOS){
            MappingEquationExam mappingEquationExam = new MappingEquationExam();
            mappingEquationExam.setExamId(eid);
            mappingEquationExam.setEquationId(equationVO.getId());
            mappingEquationExamMapper.insert(mappingEquationExam);
        }
        return equationExamVOS;
    }


    private Long getEquationId(){
        redisTemplate.opsForValue().increment("equation_id:");
        return Long.valueOf(redisTemplate.opsForValue().get("equation_id:"));
    }

}
